<?php
/*
 * @Descripttion: 
 * @Author: 荷逸
 * @email: 563428234@qq.com
 * @Date: 2021-01-21 16:46:55
 */

namespace app\admin\controller;

use app\admin\model\FilesDirectory;
use app\common\model\Attachment;
use app\common\model\AttachmentCate;
use think\Request;

class FilemanageController extends Controller
{



    public function manage(Request $request)
    {
        $param = $request->param();
        $cate = AttachmentCate::getTreeCate();
        $this->assign([
            'param' => $param,
            'cate' => json_encode($cate)
        ]);
        return $this->fetch();
    }
    //图片列表
    public function list(Request $request, Attachment $model)
    {
        $cate = $this->request->param('cate',0);
        $data = $model->field(['id', 'cate_id', 'save_name', 'original_name', 'url'])->where('cate_id',$cate)->whereIn('type', ['png', 'jpg', 'jpeg', 'gif'])->page($this->page, $this->limit)->select()->toArray();
        $count = $model->where('cate_id',$cate)->count();
        $info = [
            'data' => $data,
            'count' => $count
        ];
        return success('获取成功', '', $info);
    }

    //删除图片
    public function del(Attachment $model)
    {
        if ($this->request->isPost()) {
            $ids = $this->request->param('ids');
            if ($model->softDelete) {
                $result = $model->whereIn('id', $ids)->useSoftDelete('delete_time', time())->delete();
            } else {
                $result = $model->whereIn('id', $ids)->delete();
            }
            return $result ? success('删除成功', URL_RELOAD) : error();
        }
    }
}
